package com.example.toy.core.utils;

import com.google.common.base.Objects;
import lombok.SneakyThrows;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.net.URI;

/**
 * PathUtils
 *
 * @author shenbing
 * @date 2021/12/27
 */
public class PathUtils {

  /**
   * 判断两个路径是否一致
   * @param p1
   * @param p2
   * @return  true如果路径位置相同，如果2个路径都存在scheme，则必须保证一致，否则忽略scheme判断
   */
  @SneakyThrows
  public static boolean isSamePath(Path p1, Path p2) {
    URI u1 = p1.toUri();
    URI u2 = p2.toUri();
    boolean pathEqual = Objects.equal(u1.getPath(), u2.getPath());
    if (u1.getScheme() == null || u2.getScheme() == null) {
        return pathEqual;
    } else {
        return Objects.equal(u1.getScheme(), u2.getScheme()) && pathEqual;
    }
  }


  public static void main(String[] args) {
    Path p1 = new Path("file:///a/b") ,p2 = new Path("io:///a/b");
    System.out.println(isSamePath(p1, p2));
    System.out.println(p1.equals(p2));
  }
}
